package cn.iocoder.yudao.module.call.controller.admin.speech;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.speech.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.speech.SpeechDO;
import cn.iocoder.yudao.module.call.service.speech.SpeechService;

@Tag(name = "管理后台 - 话术")
@RestController
@RequestMapping("/call/speech")
@Validated
public class SpeechController {

    @Resource
    private SpeechService speechService;

    @PostMapping("/create")
    @Operation(summary = "创建话术")
    @PreAuthorize("@ss.hasPermission('call:speech:create')")
    public CommonResult<Long> createSpeech(@Valid @RequestBody SpeechSaveReqVO createReqVO) {
        return success(speechService.createSpeech(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新话术")
    @PreAuthorize("@ss.hasPermission('call:speech:update')")
    public CommonResult<Boolean> updateSpeech(@Valid @RequestBody SpeechSaveReqVO updateReqVO) {
        speechService.updateSpeech(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除话术")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:speech:delete')")
    public CommonResult<Boolean> deleteSpeech(@RequestParam("id") Long id) {
        speechService.deleteSpeech(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得话术")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:speech:query')")
    public CommonResult<SpeechRespVO> getSpeech(@RequestParam("id") Long id) {
        SpeechDO speech = speechService.getSpeech(id);
        return success(BeanUtils.toBean(speech, SpeechRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得话术列表")
    @OperateLog(enable = false)
    public CommonResult<List<SpeechRespVO>> getSpeechList() {
        SpeechPageReqVO pageReqVO = new SpeechPageReqVO();
        pageReqVO.setPageNo(1);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpeechDO> speech = speechService.getSpeechPage(pageReqVO).getList();
        return success(BeanUtils.toBean(speech, SpeechRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得话术分页")
    @PreAuthorize("@ss.hasPermission('call:speech:query')")
    public CommonResult<PageResult<SpeechRespVO>> getSpeechPage(@Valid SpeechPageReqVO pageReqVO) {
        PageResult<SpeechDO> pageResult = speechService.getSpeechPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpeechRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出话术 Excel")
    @PreAuthorize("@ss.hasPermission('call:speech:export')")
    @OperateLog(type = EXPORT)
    public void exportSpeechExcel(@Valid SpeechPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpeechDO> list = speechService.getSpeechPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "话术.xls", "数据", SpeechRespVO.class,
                        BeanUtils.toBean(list, SpeechRespVO.class));
    }

}